package screens
{
	import com.greensock.easing.Back;
	import com.greensock.loading.data.ImageLoaderVars;
	import com.greensock.plugins.TintPlugin;
	import events.NavigationEvent;
	import Loadtext.LoadText;
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import objects.Bartrack;
	import objects.BaseBuild;
	import objects.BaseBuild;
	import objects.Canon;
	import objects.Enemy;
	import objects.LoadEnemy;
	import objects.MyObject;
	import objects.Path;
	import objects.Solider;
	import objects.SoliderChinese;
	import objects.Star_Effect;
	import objects.TestAnimation;
	import objects.Tower;
	import starling.animation.DelayedCall;
	import starling.core.Starling;
	import starling.display.BlendMode;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.extensions.PDParticleSystem;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.VAlign;
	import starling.utils.HAlign;
	import Assets;
	import objects.ManagerObject;
	import objects.ManagerEffect;
	import objects.Road;
	import starling.filters.BlurFilter;
	import starling.events.EnterFrameEvent;
	import flash.external.ExternalInterface;
	
	/**
	 * ...
	 * @author Abiyasa
	 */
	
	public class GamePlay extends Sprite
	{
		public var m_ImageButtonBack:Image;
		public var m_testInit:Boolean;
		public var m_testLoad:Boolean;
		protected var m_tower:Tower;
		private var m_background:Image;
		private var m_background1:Image;
		private var m_enemy:Enemy;
		private var m_list_path:Array;
		;
		private var m_listEnemy:Array;
		private var m_listTower:Array;
		private var m_listCanon:Array;
		private var m_listBartrack:Array;
		private var m_listSolider:Array;
		
		private var m_listBaseBuild:Array;
		private var m_testArray:Array;
		
		private var m_mapPath:LoadText;
		
		public var m_database:database;
		public var m_testStart:Boolean;
		public var m_test:TestAnimation;
		private var m_moveMovie:MovieClip;
		//road
		public var m_Road:Road;
		
		//test wave
		public var m_Wave:int;
		public var m_currentWave:int;
		public var m_maxWave:int //get from database
		public var m_maxPath:int //get from database
		
		//load enemy
		public var m_loadEnemy:LoadEnemy;
		
		public var m_coint:int;
		private var ScoreText:TextField;
		private var m_heartText:TextField;
		public var m_testText:TextField;
		
		public var m_iconHero:Image;
		public var m_numberHeart:Number;
		public var m_result:Boolean;
		public var m_khungmaucoin:Image;
		public var m_coinImage:Image;
		public var m_star:int;
		public var m_testUpLevel:Boolean;
		public var m_heartImage:Image;
		public var m_playButton:Image;
		public var m_testplaybutton:Boolean;
		public var m_alertPlay:Image;
		public var m_starWin:Image;
		public var m_countstar:Boolean;
		
		public var m_winSprite:Sprite;
		public var m_winTextTitle:TextField;
		public var m_winTextButtonNext:TextField;
		public var m_winButtonNext:Image;
		public var m_winBackGround:Image;
		
		public var m_loseSprite:Sprite;
		public var m_loseTextTitle:TextField;
		public var m_loseTextButton:TextField;
		public var m_loseButton:Image;
		public var m_loseTextButtonReTry:TextField;
		public var m_loseButtonReTry:Image;
		public var m_loseBackGround:Image;
		
		public var m_pauseSprite:Sprite;
		public var m_pauseTextTitle:TextField;
		public var m_pauseTextButton:TextField;
		public var m_pauseButton:Image;
		public var m_pauseTextButtonReTry:TextField;
		public var m_pauseButtonReTry:Image;
		public var m_pauseBackGround:Image;
		
		public var m_rung:int;
		public var m_rung1:int;
		public var m_rung2:int;
		public var m_rung3:int;
		public var m_rung4:int;
		public var m_rung5:int;
		public var m_rung6:int;
		public var m_rung7:int;
		public var m_rung8:int;
		public var m_rung9:int;
		
		public var m_guidBall:Image;
		public var m_guidImage:Image;
		public var m_guidText:TextField;
		public var m_guidImage1:Image;
		public var m_guidText1:TextField;
		public var m_guidindex:int;
		public var m_guidBur:Number;
		
		public var m_test_Pause:Boolean;
		
		//test html
		public var m_texthtml : TextField;
		
		public function GamePlay()
		{
			super();
			m_test_Pause = false;
			m_guidindex = 1;
			m_mapPath = new LoadText("Map_Path");
			m_Wave = 1;
			m_currentWave = 1;
			m_guidBur = 0.1;
			m_list_path = new Array();
			//m_database = new database();
			this.addEventListener(Event.ADDED_TO_STAGE, init);
			this.addEventListener(Event.ENTER_FRAME, update);
			
			this.addEventListener(TouchEvent.TOUCH, onTouch);
		
		}
		
		private function update(e:Event):void
		{
			if (database.instance.m_testGuid == true)
			{
				setPos(m_guidImage, m_guidBall.x - 96, m_guidBall.y - 73);
				setPos(m_guidImage1, m_guidImage.x, m_guidImage.y - 96);
				setPos(m_guidText, m_guidImage.x, m_guidImage.y);
				setPos(m_guidText1, m_guidImage1.x, m_guidImage1.y);
				if (m_guidBall.alpha == 1)
				{
					m_guidBur *= -1;
				}
				if (m_guidBall.alpha == 0)
				{
					m_guidBur *= -1;
				}
				m_guidBall.alpha += m_guidBur;
			}
			ManagerEffect.instance.update(Global.FRAME);
			if (m_mapPath.m_LoadComplete == true && m_testInit == true)
			{
				m_coint = m_mapPath.m_Coint;
				m_numberHeart = m_mapPath.m_Head;
				createBox();
				m_testUpLevel = false;
				m_mapPath.m_LoadComplete = false;
				m_testLoad = true;
				m_testInit = false;
				setDefaultSprite();
			}
			if (m_numberHeart <= 0)
			{
				m_heartText.text = "" + m_numberHeart;
				m_testStart = false;
			}
			if (m_numberHeart <= 0)
			{
				m_testStart = false;
				loseGame();
			}
			if (m_numberHeart > 0 && m_listEnemy.length <= 0 && m_Wave > m_maxWave)
				//if (m_numberHeart <20 )
			{
				m_testStart = false;
				var tile:Number =  m_numberHeart / m_mapPath.m_Head;
				if (tile>0.6)
				{
					winGame();
					if (m_countstar == false){
					if (database.instance.m_curLevel==0)
					{
						m_star = 3;
						m_starWin = new Image(Assets.getSpriteSheet().getTexture("star_4"));
						m_starWin.pivotX = m_starWin.width / 2;
						m_starWin.pivotY = m_starWin.height / 2;
						m_starWin.x = 1050 / 2;
						m_starWin.y = 320;
						m_winSprite.addChild(m_starWin);
					}
					else
					{
						m_starWin = new Image(Assets.getSpriteSheet().getTexture("star_4"));
						m_star = 3;
						m_starWin.pivotX = m_starWin.width / 2;
						m_starWin.pivotY = m_starWin.height / 2;
						m_starWin.x = 1050 / 2;
						m_starWin.y = 320;
						m_winSprite.addChild(m_starWin);
					}
					}
					m_countstar = true;
					
					
				}
				else if (tile >= 0.3 && tile <= 0.6)
				{
					winGame();
					if (m_countstar == false){
					if (database.instance.m_curLevel==0)
					{
						m_star = 3;
						m_starWin = new Image(Assets.getSpriteSheet().getTexture("star_4"));
						m_starWin.pivotX = m_starWin.width / 2;
						m_starWin.pivotY = m_starWin.height / 2;
						m_starWin.x = 1050 / 2;
						m_starWin.y = 320;
						m_winSprite.addChild(m_starWin);
					}
					else
					{
						m_starWin = new Image(Assets.getSpriteSheet().getTexture("star_3"));
						m_star = 2;
						m_starWin.pivotX = m_starWin.width / 2;
						m_starWin.pivotY = m_starWin.height / 2;
						m_starWin.x = 1050 / 2;
						m_starWin.y = 320;
						m_winSprite.addChild(m_starWin);
					}
					}
					m_countstar = true;
					
					
				}
				else if (tile<0.3)
				{
					winGame();
					if (m_countstar == false){
						if (database.instance.m_curLevel==0)
						{
							m_star = 3;
							m_starWin = new Image(Assets.getSpriteSheet().getTexture("star_4"));
							m_starWin.pivotX = m_starWin.width / 2;
							m_starWin.pivotY = m_starWin.height / 2;
							m_starWin.x = 1050 / 2;
							m_starWin.y = 320;
							m_winSprite.addChild(m_starWin);
						}
						else
						{
							m_starWin = new Image(Assets.getSpriteSheet().getTexture("star_2"));
							m_star = 1;
							m_starWin.pivotX = m_starWin.width / 2;
							m_starWin.pivotY = m_starWin.height / 2;
							m_starWin.x = 1050 / 2;
							m_starWin.y = 320;
							m_winSprite.addChild(m_starWin);
						}
					}
					m_countstar = true;
					
					
				}
				setStar(m_star);
			}
			ZoomSprite();
			if (this.scaleX == 1)
			{
				m_rung = rung(this, m_rung, 0);
				if (m_rung == 0)
				{
					ZoomSpriteObj(m_playButton);
					if (m_playButton.scaleX == 1)
					{
						m_rung1 = rung(m_playButton, m_rung1, 1);
					}
				}
			}
			////// ************** UPDATE**************//////////////
			if (m_test_Pause == true)
			{
				pauseGame();
			}
			if (m_testLoad == true && m_testStart == true && m_testplaybutton == true && m_test_Pause == false )
			{
				ScoreText.text = "" + m_coint;
				m_heartText.text = "" + m_numberHeart;
				//update tower
				for (var i:int = m_listTower.length - 1; i >= 0; i--)
				{
					m_listTower[i].update(Global.FRAME);
					m_coint = m_listTower[i].levelUpAndSale(Global.FRAME, m_coint);
					m_listTower[i].m_cointTotal = m_coint;
					if (m_listTower[i].m_cicleItem.m_result == 2)
					{
						m_listBaseBuild.push(new BaseBuild(this, new Point(m_listTower[i].m_spriteMovie.x, m_listTower[i].m_spriteMovie.y), m_coint));
						m_listTower[i].release();
						this.removeChild(m_listTower[i]);
						m_listTower.splice(i, 1);
					}
				}
				//update canon
				for (i = m_listCanon.length - 1; i >= 0; i--)
				{
					m_listCanon[i].update(Global.FRAME);
					m_coint = m_listCanon[i].levelUpAndSale(Global.FRAME, m_coint);
					m_listCanon[i].m_cointTotal = m_coint;
					if (m_listCanon[i].m_cicleItem.m_result == 2)
					{
						m_listBaseBuild.push(new BaseBuild(this, new Point(m_listCanon[i].m_spriteMovie.x, m_listCanon[i].m_spriteMovie.y), m_coint));
						m_listCanon[i].releaseFull(m_listCanon, i);
					}
				}
				//update bartracks
				for (i = m_listBartrack.length - 1; i >= 0; i--)
				{
					m_listBartrack[i].update(Global.FRAME);
					m_coint = m_listBartrack[i].levelUpAndSale(Global.FRAME, m_coint);
					m_listBartrack[i].m_cointTotal = m_coint;
					if (m_listBartrack[i].m_cicleItem.m_result == 2)
					{
						m_listBartrack[i].release();
						m_listBaseBuild.push(new BaseBuild(this, new Point(m_listBartrack[i].m_spriteMovie.x, m_listBartrack[i].m_spriteMovie.y + 20), m_coint));
						this.removeChild(m_listBartrack[i]);
						m_listBartrack.splice(i, 1);
					}
				}
				
				//tao tru
				
				for (i = 0; i < m_listBaseBuild.length; i++)
				{
					m_listBaseBuild[i].update(Global.FRAME);
					m_coint = m_listBaseBuild[i].buyHouse(Global.FRAME, m_coint);
					m_listBaseBuild[i].m_cointTotal = m_coint;
					if (m_listBaseBuild[i].m_cicleItem.m_result == 1)
					{
						
						m_listTower.push(new Tower(this, new Point(m_listBaseBuild[i].m_spriteImage.x, m_listBaseBuild[i].m_spriteImage.y), m_coint));
						m_listBaseBuild[i].m_cicleItem.m_result = 0;
						
						this.removeChild(m_listBaseBuild[i]);
						m_listBaseBuild.splice(i, 1);
						
					}
					else if (m_listBaseBuild[i].m_cicleItem.m_result == 3)
					{
						
						m_listCanon.push(new Canon(this, new Point(m_listBaseBuild[i].m_spriteImage.x, m_listBaseBuild[i].m_spriteImage.y), m_coint));
						m_listBaseBuild[i].m_cicleItem.m_result = 0;
						this.removeChild(m_listBaseBuild[i]);
						m_listBaseBuild.splice(i, 1);
					}
					else if (m_listBaseBuild[i].m_cicleItem.m_result == 2)
					{
						
						m_listBartrack.push(new Bartrack(this, new Point(m_listBaseBuild[i].m_spriteImage.x, m_listBaseBuild[i].m_spriteImage.y), m_Road, m_coint));
						m_listBaseBuild[i].m_cicleItem.m_result = 0;
						this.removeChild(m_listBaseBuild[i]);
						m_listBaseBuild.splice(i, 1);
					}
					
				}
				
				//update enemy
				for (i = m_listEnemy.length - 1; i >= 0; i--)
				{
					m_listEnemy[i].update(Global.FRAME);
					if (m_listEnemy[i].m_hp <= 0)
					{
						if (m_listEnemy[i].m_EnemyDestination == true)
						{
							m_numberHeart -= 1;
						}
						else if (m_listEnemy[i].m_EnemyDestination == false)
						{
					    	m_coint += m_listEnemy[i].m_coinEnemy;
						}
						m_listEnemy[i].release();
						this.removeChild(m_listEnemy[i]);
						m_listEnemy.splice(i, 1);
						continue;
					}
				}
				
				//update wave
				if (m_listEnemy.length <= 0 && m_Wave <= m_maxWave) //current wave
				{
					for (i = 0; i < m_mapPath.m_listData_Wave.length; i++)
					{
						if (m_mapPath.m_listData_Wave[i].wave == m_Wave)
						{
							m_loadEnemy.pushEnemy(this, new Point(m_mapPath.m_listData_Wave[i].x, m_mapPath.m_listData_Wave[i].y), m_list_path[m_mapPath.m_listData_Wave[i].path], m_mapPath.m_listData_Wave[i].id);
							
						}
					}
					m_Wave++;
				}
				////// ************** COLLISION**************//////////////
				//effect with enemy
				ManagerEffect.instance.CollisionWithArray(m_listEnemy, Global.FRAME);
				
				//collision between tower and enemy
				for (var j:int = 0; j < m_listTower.length; j++)
				{
					for (i = 0; i < m_listEnemy.length; i++)
					{
						if (m_listTower[j].m_collision == true)
							continue;
						m_listTower[j].Collision(m_listEnemy[i], (Global.FRAME));
					}
				}
				
				//collision between canon and enemy
				for (j = 0; j < m_listCanon.length; j++)
				{
					for (i = 0; i < m_listEnemy.length; i++)
					{
						if (m_listCanon[j].m_collision == true)
							continue;
						m_listCanon[j].Collision(m_listEnemy[i], (Global.FRAME));
					}
				}
				
				//collision between bartrack and enemy
				for (i = 0; i < m_listBartrack.length; i++)
				{
					for (j = 0; j < m_listEnemy.length; j++)
					{
						
						m_listBartrack[i].Collision(m_listEnemy[j], (Global.FRAME));
					}
				}
				/*//collision between enemy and solider
				   for (var i:int = 0 ; i < m_listEnemy.length ; i++ )
				   {
				   for (var j:int = 0; j < m_listSolider.length; j++)
				   {
				   m_listEnemy[i].Collision(m_listSolider[j], (Global.FRAME));
				   }
				 }*/
			}
			if (database.instance.m_testGuid == false)
			{
				this.setChildIndex(m_background1, this.numChildren);
				this.setChildIndex(m_playButton, this.numChildren);
				this.setChildIndex(m_alertPlay, this.numChildren);
			}
			
		}
		
		private function onTouch(e:TouchEvent):void
		{
			
			
			var touch:Touch = e.getTouch(stage);
			if (touch)
			{
				var m_touchPoint:Point = new Point(touch.globalX, touch.globalY);
				if (database.instance.m_testGuid == true)
				{
					this.setChildIndex(this.m_guidBall, this.numChildren);
					this.setChildIndex(this.m_guidImage, this.numChildren);
					this.setChildIndex(this.m_guidText, this.numChildren);
					if (m_guidBall.bounds.containsPoint(m_touchPoint))
					{
						database.instance.m_testTouch = true;
						if (touch.phase == TouchPhase.ENDED)
						{
							if (m_guidindex == 1 && m_guidBall.x == 1000)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 403, 425);
								m_guidText.text = "Chọn biểu tượng nền móng";
								m_guidText1.text = "Xây nhà";
							}else
							if (m_guidindex == 2 && m_guidBall.x == 403)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 350, 380);
								m_guidText.text = "Chọn xây nhà cung";
								m_guidText1.text = "Xây nhà";
							}else
							if (m_guidindex == 3 && m_guidBall.x == 350)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 403, 425);
								m_guidText.text = "Chọn vào nhà cung";
								m_guidText1.text = "Nâng cấp";
							}else
							if (m_guidindex == 4 && m_guidBall.x == 403)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 403, 320);
								m_guidText.text = "Nâng cấp nhà cung";
								m_guidText1.text = "Nâng cấp";
							}else
							if (m_guidindex == 5 && m_guidBall.x == 403)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 300, 555);
								m_guidText.text = "Xây nhà";
								m_guidText1.text = "Bán nhà";
							}else
							if (m_guidindex == 6 && m_guidBall.x == 300)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 252, 499);
								m_guidText.text = "Chọn nhà xây cung";
								m_guidText1.text = "Bán nhà";
							}else
							if (m_guidindex == 7 && m_guidBall.x == 252)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 300, 555);
								m_guidText.text = "Chọn vào nhà cung";
								m_guidText1.text = "Bán nhà";
							}else
							if (m_guidindex == 8 && m_guidBall.x == 300)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 300, 655);
								m_guidText.text = "Bán nhà cung";
								m_guidText1.text = "Bán nhà";
							}else
							if (m_guidindex == 9 && m_guidBall.x == 300)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 300, 555);
								m_guidText.text = "Xây nhà";
								m_guidText1.text = "Chuyển quân";
							}else
							if (m_guidindex == 10 && m_guidBall.x == 300)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 352, 577);
								m_guidText.text = "Chọn xây nhà quân";
								m_guidText1.text = "Chuyển quân";
							}else
							if (m_guidindex == 11 && m_guidBall.x == 352)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 300, 555);
								m_guidText.text = "Chọn vào nhà quân";
								m_guidText1.text = "Chuyển quân";
							}else
							if (m_guidindex == 12 && m_guidBall.x == 300)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 363, 606);
								m_guidText.text = "Chọn di chuyển quân";
								m_guidText1.text = "Chuyển quân";
							}else
							if (m_guidindex == 13 && m_guidBall.x == 363)
							{
								m_guidindex += 1;
								setPos(m_guidBall, 414, 507);
								m_guidText.text = "Chọn vị trí cần đến";
								m_guidText1.text = "Di chuyển quân";
							}else
							if (m_guidindex == 14 && m_guidBall.x == 414)
							{
								m_guidindex += 1;
								setPos(m_guidBall, -100, -100);
								setPos(m_guidImage, m_guidBall.x - 96, m_guidBall.y - 73);
								setPos(m_guidImage1, m_guidImage.x, m_guidImage.y - 96);
								setPos(m_guidText, m_guidImage.x, m_guidImage.y);
								setPos(m_guidText1, m_guidImage1.x, m_guidImage1.y);
								database.instance.m_testGuid = false;
								database.instance.m_testTouch = true;
							}
							
							
						}
					}
					else
						database.instance.m_testTouch = false;
				}
				if (database.instance.m_testTouch == true)
				{
					if (touch.phase == TouchPhase.BEGAN)
					{
						if (m_ImageButtonBack.bounds.containsPoint(m_touchPoint) == true)
						{
							if (database.instance.m_testPlaySound == true)
								database.instance.m_Sound.playSound("Sound_Click");
							//this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "GamePlay-ChooseLevel"}, true));
							if (m_test_Pause==true)
							{
								m_test_Pause = false;
							}
							else
								m_test_Pause = true;
						}
					}
					else if (touch.phase == TouchPhase.ENDED)
					{
						if (m_playButton.bounds.containsPoint(m_touchPoint) == true && m_playButton.alpha == 1)
						{
							if (database.instance.m_testPlaySound == true)
								database.instance.m_Sound.playSound("Sound_Click");
							this.m_testplaybutton = true;
							m_playButton.alpha = 0.5;
							m_alertPlay.alpha = 0;
							
						}
						if (m_winButtonNext.bounds.containsPoint(m_touchPoint) == true && m_winSprite.visible == true)
						{
							if (database.instance.m_testPlaySound == true)
								database.instance.m_Sound.playSound("Sound_Click");
							this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, { id: "GamePlay-ChooseLevel" }, true));
							/*if(database.instance.m_curStage==0)
								switch(database.instance.m_curLevel )
								{
									case 0:
										ExternalInterface.call("openFeedDialog1");
										break;
									case 1:
										ExternalInterface.call("openFeedDialog2");
										break;								
									case 2:
										ExternalInterface.call("openFeedDialog3");
										break;								
									case 3:
										ExternalInterface.call("openFeedDialog4");
										break;								
									case 4:
										ExternalInterface.call("openFeedDialog5");
										break;								
									case 5:
										ExternalInterface.call("openFeedDialog6");
										break;								
									case 6:
										ExternalInterface.call("openFeedDialog7");
										break;								
									case 7:
										ExternalInterface.call("openFeedDialog8");
										break;								
									case 8:
										ExternalInterface.call("openFeedDialog9");
										break;								
									case 9:
										ExternalInterface.call("openFeedDialog10");
										break;								
									case 10:
										ExternalInterface.call("openFeedDialog11");
										break;
									case 11:
										ExternalInterface.call("openFeedDialog12");
										break;
								}
							if(database.instance.m_curStage==1)
								switch(database.instance.m_curLevel )
								{
									case 0:
										ExternalInterface.call("openFeedDialog13");
										break;
									case 1:
										ExternalInterface.call("openFeedDialog14");
										break;								
									case 2:
										ExternalInterface.call("openFeedDialog15");
										break;								
									case 3:
										ExternalInterface.call("openFeedDialog16");
										break;								
									case 4:
										ExternalInterface.call("openFeedDialog17");
										break;								
									case 5:
										ExternalInterface.call("openFeedDialog18");
										break;								
									case 6:
										ExternalInterface.call("openFeedDialog19");
										break;								
									case 7:
										ExternalInterface.call("openFeedDialog20");
										break;								
								}*/
							
						}
						if (m_loseButtonReTry.bounds.containsPoint(m_touchPoint) == true && m_loseSprite.visible == true)
						{
							if (database.instance.m_testPlaySound == true)
								database.instance.m_Sound.playSound("Sound_Click");
							this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "GamePlay-GamePlay"}, true));
						}
						
						if (m_loseButton.bounds.containsPoint(m_touchPoint) == true && m_loseSprite.visible == true)
						{
							if (database.instance.m_testPlaySound == true)
								database.instance.m_Sound.playSound("Sound_Click");
							this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "GamePlay-ChooseLevel"}, true));
						}
						
						if (m_pauseButtonReTry.bounds.containsPoint(m_touchPoint) == true && m_pauseSprite.visible == true)
						{
							if (database.instance.m_testPlaySound == true)
								database.instance.m_Sound.playSound("Sound_Click");
							m_test_Pause = false;
							m_pauseSprite.visible = false;
							setScale(m_pauseSprite, 0, 0);
							setScale(m_pauseButton, 0, 0);
							setScale(m_pauseTextButton, 0, 0);
							setScale(m_pauseTextButtonReTry, 0, 0);
							setScale(m_pauseButtonReTry, 0, 0);
							m_rung7 = m_rung8 = m_rung9 = 5;
						}
						
						if (m_pauseButton.bounds.containsPoint(m_touchPoint) == true && m_pauseSprite.visible == true)
						{
							if (database.instance.m_testPlaySound == true)
								database.instance.m_Sound.playSound("Sound_Click");
							this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "GamePlay-ChooseLevel"}, true));
						}
						
					}
					else if (touch.phase == TouchPhase.HOVER)
					{
						if (m_ImageButtonBack.bounds.containsPoint(m_touchPoint) == true)
						{
							m_ImageButtonBack.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
						}
						else
							m_ImageButtonBack.filter = null;
						if (m_playButton.bounds.containsPoint(m_touchPoint) == true && m_playButton.alpha == 1)
						{
							m_playButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
						}
						else
							m_playButton.filter = null;
						
						if (m_winButtonNext.bounds.containsPoint(m_touchPoint) == true && m_winSprite.visible == true)
						{
							m_winButtonNext.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
						}
						else
							m_winButtonNext.filter = null;
						
						if (m_loseButton.bounds.containsPoint(m_touchPoint) == true && m_loseSprite.visible == true)
						{
							m_loseButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
						}
						else
							m_loseButton.filter = null;
						
						if (m_loseButtonReTry.bounds.containsPoint(m_touchPoint) == true && m_loseSprite.visible == true)
						{
							m_loseButtonReTry.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
						}
						else
							m_loseButtonReTry.filter = null;
							
						if (m_pauseButton.bounds.containsPoint(m_touchPoint) == true && m_pauseSprite.visible == true)
						{
							m_pauseButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
						}
						else
							m_pauseButton.filter = null;
						
						if (m_pauseButtonReTry.bounds.containsPoint(m_touchPoint) == true && m_pauseSprite.visible == true)
						{
							m_pauseButtonReTry.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
						}
						else
							m_pauseButtonReTry.filter = null;
					}
				}
			}
		}
		
		protected function setStar(_star:int):void
		{
			switch (int(database.instance.m_curStage + 1))
			{
				case 1: 
					if (database.instance.m_curLevel == database.instance.RESULTSTARSTAGE1[0])
					{
						if (database.instance.RESULTSTARSTAGE1[database.instance.m_curLevel + 1] < _star)
						{
							database.instance.RESULTLEVELUPDATESTAR[6] =int(database.instance.RESULTLEVELUPDATESTAR[6])+ m_star - int(database.instance.RESULTSTARSTAGE1[database.instance.m_curLevel + 1])+"";
							database.instance.setDataTotalStar(database.instance.RESULTLEVELUPDATESTAR[6]);
							database.instance.RESULTSTARSTAGE1[database.instance.m_curLevel + 1] = _star + "";
							if (m_testUpLevel == false)
							{
								if (database.instance.m_curLevel != 11)
								{
									database.instance.RESULTSTARSTAGE1[0] = (int(database.instance.RESULTSTARSTAGE1[0]) + 1) + "";
									m_testUpLevel = true;
								}
								else if (database.instance.RESULTSTAGE[1] == 1)
								{
									database.instance.RESULTSTAGE[1] = 2;
									database.instance.setStage(database.instance.RESULTSTAGE[1], database.instance.RESULTSTAGE[2]);
								}
							}
							database.instance.setStarStage1(database.instance.RESULTSTARSTAGE1[0], database.instance.RESULTSTARSTAGE1[1], database.instance.RESULTSTARSTAGE1[2], database.instance.RESULTSTARSTAGE1[3], database.instance.RESULTSTARSTAGE1[4], database.instance.RESULTSTARSTAGE1[5], database.instance.RESULTSTARSTAGE1[6], database.instance.RESULTSTARSTAGE1[7], database.instance.RESULTSTARSTAGE1[8], database.instance.RESULTSTARSTAGE1[9], database.instance.RESULTSTARSTAGE1[10], database.instance.RESULTSTARSTAGE1[11], database.instance.RESULTSTARSTAGE1[12]);
						}
					}
					else if (database.instance.RESULTSTARSTAGE1[database.instance.m_curLevel + 1] < _star)
					{
						database.instance.RESULTLEVELUPDATESTAR[6] =int(database.instance.RESULTLEVELUPDATESTAR[6])+ m_star - int(database.instance.RESULTSTARSTAGE1[database.instance.m_curLevel + 1])+"";
						database.instance.setDataTotalStar(database.instance.RESULTLEVELUPDATESTAR[6]);
						database.instance.RESULTSTARSTAGE1[database.instance.m_curLevel + 1] = _star + "";
						
						database.instance.setStarStage1(database.instance.RESULTSTARSTAGE1[0], database.instance.RESULTSTARSTAGE1[1], database.instance.RESULTSTARSTAGE1[2], database.instance.RESULTSTARSTAGE1[3], database.instance.RESULTSTARSTAGE1[4], database.instance.RESULTSTARSTAGE1[5], database.instance.RESULTSTARSTAGE1[6], database.instance.RESULTSTARSTAGE1[7], database.instance.RESULTSTARSTAGE1[8], database.instance.RESULTSTARSTAGE1[9], database.instance.RESULTSTARSTAGE1[10], database.instance.RESULTSTARSTAGE1[11], database.instance.RESULTSTARSTAGE1[12]);
					}
					break;
				
				case 2: 
					if (database.instance.m_curLevel == database.instance.RESULTSTARSTAGE2[0])
					{
						if (database.instance.RESULTSTARSTAGE2[database.instance.m_curLevel + 1] < _star)
						{
							database.instance.RESULTLEVELUPDATESTAR[6] =int(database.instance.RESULTLEVELUPDATESTAR[6])+ m_star - int(database.instance.RESULTSTARSTAGE1[database.instance.m_curLevel + 1])+"";
							database.instance.setDataTotalStar(database.instance.RESULTLEVELUPDATESTAR[6]);
							database.instance.RESULTSTARSTAGE2[database.instance.m_curLevel + 1] = _star + "";
							if (m_testUpLevel == false)
							{
								if (database.instance.m_curLevel != 7)
								{
									database.instance.RESULTSTARSTAGE2[0] = (int(database.instance.RESULTSTARSTAGE2[0]) + 1) + "";
									m_testUpLevel = true;
								}
								else if (database.instance.RESULTSTAGE[1] == 2)
								{
									database.instance.RESULTSTAGE[1] = 3;
									database.instance.setStage(database.instance.RESULTSTAGE[1], database.instance.RESULTSTAGE[2]);
								}
							}
							database.instance.setStarStage2(database.instance.RESULTSTARSTAGE2[0], database.instance.RESULTSTARSTAGE2[1], database.instance.RESULTSTARSTAGE2[2], database.instance.RESULTSTARSTAGE2[3], database.instance.RESULTSTARSTAGE2[4], database.instance.RESULTSTARSTAGE2[5], database.instance.RESULTSTARSTAGE2[6], database.instance.RESULTSTARSTAGE2[7], database.instance.RESULTSTARSTAGE2[8]);
						}
					}
					else if (database.instance.RESULTSTARSTAGE2[database.instance.m_curLevel + 1] < _star)
					{
						database.instance.RESULTLEVELUPDATESTAR[6] =int(database.instance.RESULTLEVELUPDATESTAR[6])+ m_star - int(database.instance.RESULTSTARSTAGE1[database.instance.m_curLevel + 1])+"";
						database.instance.setDataTotalStar(database.instance.RESULTLEVELUPDATESTAR[6]);
						database.instance.RESULTSTARSTAGE2[database.instance.m_curLevel + 1] = _star + "";
						
						database.instance.setStarStage2(database.instance.RESULTSTARSTAGE2[0], database.instance.RESULTSTARSTAGE2[1], database.instance.RESULTSTARSTAGE2[2], database.instance.RESULTSTARSTAGE2[3], database.instance.RESULTSTARSTAGE2[4], database.instance.RESULTSTARSTAGE2[5], database.instance.RESULTSTARSTAGE2[6], database.instance.RESULTSTARSTAGE2[7], database.instance.RESULTSTARSTAGE2[8]);
					}
					break;
			}
		
		}
		
		protected function init(event:Event):void
		{
			
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			//createBox();
			//m_database.getData();	
		
		}
		
		protected function createBox():void
		{
			ManagerEffect.instance.m_layer = this;
			m_rung = 5;
			m_rung1 = 5;
			m_rung2 = 5;
			m_rung3 = 5;
			m_rung4 = 5;
			m_rung5 = 5;
			m_rung6 = 5;
			
			m_rung7 = m_rung8 = m_rung9 = 5;
			
			m_winSprite = new Sprite();
			m_loseSprite = new Sprite();
			m_pauseSprite = new Sprite();
			m_countstar = false;
			
			m_ImageButtonBack = new Image(Assets.getTexture("PAUSE"));
			m_ImageButtonBack.pivotX = m_ImageButtonBack.width / 2;
			m_ImageButtonBack.pivotY = m_ImageButtonBack.height / 2;
			m_ImageButtonBack.width = 50;
			m_ImageButtonBack.height = 50;
			m_ImageButtonBack.x = 1025;
			m_ImageButtonBack.y = 25;
			if (database.instance.m_curStage == 0)
			{
				switch (database.instance.m_curLevel)
				{
					case 0: 
						m_background = new Image(Assets.getTexture("BackGroundlv0"));
						break;
					case 1: 
						m_background = new Image(Assets.getTexture("BackGroundlv2"));
						break;
					case 2: 
						m_background = new Image(Assets.getTexture("BackGroundlv3"));
						break;
					case 3: 
						m_background = new Image(Assets.getTexture("BackGroundlv4"));
						break;
					case 4: 
						m_background = new Image(Assets.getTexture("BackGroundlv5"));
						break;
					case 5: 
						m_background = new Image(Assets.getTexture("BackGroundlv6"));
						break;
					case 6: 
						m_background = new Image(Assets.getTexture("BackGroundlv7"));
						break;
					case 7: 
						m_background = new Image(Assets.getTexture("BackGroundlv8"));
						break;
					case 8: 
						m_background = new Image(Assets.getTexture("BackGroundlv9"));
						break;
					case 9: 
						m_background = new Image(Assets.getTexture("BackGroundlv10"));
						break;
					case 10: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
					case 11: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
				
				}
			}
			if (database.instance.m_curStage == 1)
			{
				switch (database.instance.m_curLevel)
				{
					case 0: 
						m_background = new Image(Assets.getTexture("BackGroundlv0"));
						break;
					case 1: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
					case 2: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
					case 3: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
					case 4: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
					case 5: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
					case 6: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
					case 7: 
						m_background = new Image(Assets.getTexture("BackGroundlv1"));
						break;
				
				}
			}
			//m_background.blendMode = BlendMode.NONE;
			this.addChild(m_background);
			m_background1 = new Image(Assets.getTexture("background6"));
			//m_background.blendMode = BlendMode.NONE;
			
			m_khungmaucoin = new Image(Assets.getSpriteSheet().getTexture("khungmaucoin"));
			m_khungmaucoin.pivotX = m_khungmaucoin.width / 2;
			m_khungmaucoin.pivotY = m_khungmaucoin.height / 2;
			m_khungmaucoin.scaleX = 0.6;
			m_khungmaucoin.scaleY = 0.6;
			m_khungmaucoin.x = 150;
			m_khungmaucoin.y = 40;
			this.addChild(m_khungmaucoin);
			
			m_coinImage = new Image(Assets.getSpriteSheet().getTexture("coin"));
			m_coinImage.pivotX = m_coinImage.width / 2;
			m_coinImage.pivotY = m_coinImage.height / 2;
			m_coinImage.scaleX = 0.4;
			m_coinImage.scaleY = 0.4;
			m_coinImage.x = m_khungmaucoin.x + 10;
			m_coinImage.y = m_khungmaucoin.y;
			this.addChild(m_coinImage);
			
			m_heartImage = new Image(Assets.getSpriteSheet().getTexture("heart"));
			m_heartImage.pivotX = m_heartImage.width / 2;
			m_heartImage.pivotY = m_heartImage.height / 2;
			m_heartImage.scaleX = 0.3;
			m_heartImage.scaleY = 0.3;
			m_heartImage.x = m_khungmaucoin.x - 60;
			m_heartImage.y = m_khungmaucoin.y;
			this.addChild(m_heartImage);
			
			m_loadEnemy = new LoadEnemy();
			
			//LIST SOLIDER
			m_listSolider = new Array();
			ManagerObject.Instance().setListSolider(m_listSolider);
			m_listSolider = ManagerObject.Instance().getListSolider();
			
			//list enemy
			/*			m_listEnemy = new Array();
			 ManagerObject.Instance().setListEnemy (m_listEnemy);*/
			m_listEnemy = ManagerObject.Instance().getListEnemy();
			
			//list tower
			m_listTower = new Array();
			ManagerObject.Instance().setListTower(m_listTower);
			m_listTower = ManagerObject.Instance().getListTower();
			
			//m_listTower.push (new Tower(this, new Point (362, 210)));
			//m_listTower.push (new Tower(this, new Point (400, 422)));
			
			//list canon
			m_listCanon = new Array();
			ManagerObject.Instance().setListCanon(m_listCanon);
			m_listCanon = ManagerObject.Instance().getListCanon();
			
			//m_listCanon.push (new Canon(this, new Point (300, 300)));
			
			//list base build
			m_listBaseBuild = new Array();
			ManagerObject.Instance().setListBaseBuild(m_listBaseBuild);
			m_listBaseBuild = ManagerObject.Instance().getListBaseBuild();
			for (var i:int = 0; i < m_mapPath.m_listData_Base.length; i++)
			{
				m_listBaseBuild.push(new BaseBuild(this, m_mapPath.m_listData_Base[i], m_coint));
			}
			
			//road 
			
			m_Road = new Road();
			for (i = 0; i < m_mapPath.m_listData_Rect.length; i++)
			{
				m_Road.addNode(m_mapPath.m_listData_Rect[i]);
			}
			
			//list bartrack
			m_listBartrack = new Array();
			ManagerObject.Instance().setListBartrack(m_listBartrack);
			m_listBartrack = ManagerObject.Instance().getListBartrack();
			
			m_maxWave = m_mapPath.m_listData_Wave[m_mapPath.m_listData_Wave.length - 1].wave;
			m_maxPath = m_mapPath.m_listData_Path[m_mapPath.m_listData_Path.length - 1].path;
			//Path
			for (i = 0; i <= m_maxPath; i++)
			{
				var path:Path = new Path();
				m_list_path.push(path);
			}
			//path left
			
			for (i = 0; i < m_mapPath.m_listData_Path.length; i++)
			{
				m_list_path[m_mapPath.m_listData_Path[i].path].addNode(new Point(m_mapPath.m_listData_Path[i].x, m_mapPath.m_listData_Path[i].y));
			}
			
			ScoreText = new TextField(300, 100, "0", "MyFontName", 24, 0x332616);
			ScoreText.pivotX = ScoreText.width / 2;
			ScoreText.pivotY = ScoreText.height / 2;
			ScoreText.scaleX = 0.9;
			ScoreText.scaleY = 0.9;
			
			ScoreText.x = m_coinImage.x + 40;
			ScoreText.y = m_coinImage.y;
			
			m_heartText = new TextField(300, 200, "0", "MyFontName", 24, 0x332616);
			m_heartText.pivotX = m_heartText.width / 2;
			m_heartText.pivotY = m_heartText.height / 2;
			m_heartText.scaleX = 0.9;
			m_heartText.scaleY = 0.9;
			m_heartText.x = m_heartImage.x + 25;
			m_heartText.y = m_heartImage.y;
			
			m_playButton = new Image(Assets.getTexture("Play"));
			m_playButton.pivotX = m_playButton.width / 2;
			m_playButton.pivotY = m_playButton.height / 2;
			m_playButton.x = 1000;
			m_playButton.y = 670;
			
			m_alertPlay = new Image(Assets.getSpriteSheet().getTexture("Play"));
			m_alertPlay.pivotX = m_alertPlay.width / 2;
			m_alertPlay.pivotY = m_alertPlay.height / 2;
			m_alertPlay.scaleX = 0.5;
			m_alertPlay.scaleY = 0.5;
			m_alertPlay.x = m_playButton.x - 50;
			m_alertPlay.y = m_playButton.y - 40;
			
			this.addChild(ScoreText);
			this.addChild(m_heartText);
			
			this.addChild(m_background1);
			this.addChild(m_ImageButtonBack);
			
			this.addChild(m_playButton);
			this.addChild(m_alertPlay);
			if (database.instance.m_testPlayMusic == true)
				database.instance.m_Music.playSound("GamePlay_Music", 0.5, 999);
			
			//khoi tao win
			m_winTextTitle = new TextField(500, 400, "", "MyFontName", 40, 0x332616);
			m_winBackGround = new Image(Assets.getTexture("Khungwin"));
			
			m_winTextButtonNext = new TextField(500, 50, "", "MyFontName", 25, 0x332616);
			m_winTextButtonNext.text = "Tiếp tục";
			m_winButtonNext = new Image(Assets.getTexture("Khung"));

			m_winTextTitle.hAlign = "left";
			setCenterPivot(m_winTextTitle);
			setPos(m_winTextTitle, 1050 / 2, 250);
			
			m_winTextTitle.text = "               Chiến thắng";
			
			setCenterPivot(m_winButtonNext);
			setPos(m_winButtonNext, 1050 / 2, 700 / 2 + 50);
			
			setCenterPivot(m_winTextButtonNext);
			setPos(m_winTextButtonNext, m_winButtonNext.x, m_winButtonNext.y);
			
			setCenterPivot(m_winBackGround);
			setPos(m_winBackGround, 1050 / 2, 700 / 2);
			
			setSize(m_winSprite, 1050, 700);
			setCenterPivotXY(m_winSprite, 1050 / 2, 700 / 2);
			setPos(m_winSprite, 1050 / 2, 700 / 2);
			setScale(m_winSprite, 0.1, 0.1);
			
			m_winSprite.addChild(m_winBackGround);
			m_winSprite.addChild(m_winTextTitle);
			m_winSprite.addChild(m_winButtonNext);
			m_winSprite.addChild(m_winTextButtonNext);
			
			this.addChild(m_winSprite);
			m_winSprite.visible = false;
			
			//khoi tao lose
			m_loseTextTitle = new TextField(500, 400, "               Thua cuộc", "MyFontName", 40, 0x332616);
			m_loseBackGround = new Image(Assets.getTexture("Khungwin"));
			
			m_loseTextButton = new TextField(500, 50, "", "MyFontName", 25, 0x332616);
			m_loseTextButton.text = "Trở về";
			m_loseButton = new Image(Assets.getTexture("Khung"));
			
			m_loseTextButtonReTry = new TextField(500, 50, "", "MyFontName", 25, 0x332616);
			m_loseTextButtonReTry.text = "Thử lại";
			m_loseButtonReTry = new Image(Assets.getTexture("Khung"));
			
			m_loseTextTitle.hAlign = "left";
			setCenterPivot(m_loseTextTitle);
			setPos(m_loseTextTitle, 1050 / 2, 300);
			
			setCenterPivot(m_loseButton);
			setPos(m_loseButton, 1050 / 2 - 100, 700 / 2 + 50);
			
			setCenterPivot(m_loseTextButton);
			setPos(m_loseTextButton, m_loseButton.x, m_loseButton.y);
			
			setCenterPivot(m_loseButtonReTry);
			setPos(m_loseButtonReTry, 1050 / 2 + 100, 700 / 2 + 50);
			
			setCenterPivot(m_loseTextButtonReTry);
			setPos(m_loseTextButtonReTry, m_loseButtonReTry.x, m_loseButtonReTry.y);
			
			setCenterPivot(m_loseBackGround);
			setPos(m_loseBackGround, 1050 / 2, 700 / 2);
			
			setSize(m_loseSprite, 1050, 700);
			setCenterPivotXY(m_loseSprite, 1050 / 2, 700 / 2);
			setPos(m_loseSprite, 1050 / 2, 700 / 2);
			setScale(m_loseSprite, 0.1, 0.1);
			
			m_loseSprite.addChild(m_loseBackGround);
			m_loseSprite.addChild(m_loseTextTitle);
			m_loseSprite.addChild(m_loseButton);
			m_loseSprite.addChild(m_loseTextButton);
			m_loseSprite.addChild(m_loseButtonReTry);
			m_loseSprite.addChild(m_loseTextButtonReTry);
			
			this.addChild(m_loseSprite);
			m_loseSprite.visible = false;
			
			
			//khoi tao pause
			m_pauseTextTitle = new TextField(500, 400, "               Tạm dừng", "MyFontName", 40, 0x332616);
			m_pauseBackGround = new Image(Assets.getTexture("Khungwin"));
			
			m_pauseTextButton = new TextField(500, 50, "", "MyFontName", 25, 0x332616);
			m_pauseTextButton.text = "Thoát trò chơi";
			m_pauseButton = new Image(Assets.getTexture("Khung"));
			
			m_pauseTextButtonReTry = new TextField(500, 50, "", "MyFontName", 25, 0x332616);
			m_pauseTextButtonReTry.text = "Tiếp tục";
			m_pauseButtonReTry = new Image(Assets.getTexture("Khung"));
			
			m_pauseTextTitle.hAlign = "left";
			setCenterPivot(m_pauseTextTitle);
			setPos(m_pauseTextTitle, 1050 / 2, 300);
			
			setCenterPivot(m_pauseButton);
			setPos(m_pauseButton, 1050 / 2 - 100, 700 / 2 + 50);
			
			setCenterPivot(m_pauseTextButton);
			setPos(m_pauseTextButton, m_pauseButton.x, m_pauseButton.y);
			
			setCenterPivot(m_pauseButtonReTry);
			setPos(m_pauseButtonReTry, 1050 / 2 + 100, 700 / 2 + 50);
			
			setCenterPivot(m_pauseTextButtonReTry);
			setPos(m_pauseTextButtonReTry, m_pauseButtonReTry.x, m_pauseButtonReTry.y);
			
			setCenterPivot(m_pauseBackGround);
			setPos(m_pauseBackGround, 1050 / 2, 700 / 2);
			
			setSize(m_pauseSprite, 1050, 700);
			setCenterPivotXY(m_pauseSprite, 1050 / 2, 700 / 2);
			setPos(m_pauseSprite, 1050 / 2, 700 / 2);
			setScale(m_pauseSprite, 0.1, 0.1);
			
			m_pauseSprite.addChild(m_pauseBackGround);
			m_pauseSprite.addChild(m_pauseTextTitle);
			m_pauseSprite.addChild(m_pauseButton);
			m_pauseSprite.addChild(m_pauseTextButton);
			m_pauseSprite.addChild(m_pauseButtonReTry);
			m_pauseSprite.addChild(m_pauseTextButtonReTry);
			
			this.addChild(m_pauseSprite);
			m_pauseSprite.visible = false;
			
			
			
			if (database.instance.m_curLevel == 0&&database.instance.m_curStage==0)
			{
				database.instance.m_testGuid = true;
				m_guidBall = new Image(Assets.getTexture("chuthich"));
				m_guidImage = new Image(Assets.getTexture("chuthichkhung"));
				setCenterPivot(m_guidImage);
				m_guidText = new TextField(166, 124, "Ấn để chơi", "MyFontName", 24, 0x332616);
				setCenterPivot(m_guidText);
				m_guidImage1 = new Image(Assets.getTexture("chuthichkhung1"));
				setCenterPivot(m_guidImage1);
				m_guidText1 = new TextField(166, 124, "Bắt đầu", "MyFontName", 20, 0x332616);
				setCenterPivot(m_guidText1);
				setPos(m_guidBall, 1000, 670);
				setCenterPivot(m_guidBall);
				this.addChild(m_guidBall);
				this.addChild(m_guidImage);
				this.addChild(m_guidText);
				this.addChild(m_guidImage1);
				this.addChild(m_guidText1);
				
				m_guidBall.visible = true;
			}
			else
			{
				database.instance.m_testGuid = false;
				database.instance.m_testTouch = true;
			}
		}
		
		public function winGame():void
		{
			this.setChildIndex(m_winSprite, this.numChildren);
			database.instance.m_testGuid = false;
			database.instance.m_testTouch = true;
			m_winSprite.visible = true;
			ZoomSpriteOB(m_winSprite);
			if (m_winSprite.scaleX == 1)
			{
				m_rung2 = rung(m_winSprite, m_rung2, 2);
				ZoomSpriteOB(m_winButtonNext);
				ZoomSpriteOB(m_winTextButtonNext);
				if (m_winButtonNext.scaleX == 1)
				{
					m_rung3 = rung(m_winButtonNext, m_rung3, 1);
				}
			}
		}
		
		public function loseGame():void
		{
			
			this.setChildIndex(m_loseSprite, this.numChildren);
			database.instance.m_testGuid = false;
			database.instance.m_testTouch = true;
			m_loseSprite.visible = true;
			ZoomSpriteOB(m_loseSprite);
			if (m_loseSprite.scaleX == 1)
			{
				m_rung2 = rung(m_loseSprite, m_rung2, 3);
				ZoomSpriteOB(m_loseButton);
				ZoomSpriteOB(m_loseTextButton);
				ZoomSpriteOB(m_loseButtonReTry);
				ZoomSpriteOB(m_loseTextButtonReTry);
				if (m_loseButton.scaleX == 1)
				{
					m_rung3 = rung(m_loseButton, m_rung3, 1);
					m_rung4 = rung(m_loseButtonReTry, m_rung4, 1);
				}
			}
		}
		public function pauseGame():void
		{
			
			this.setChildIndex(m_pauseSprite, this.numChildren);
			database.instance.m_testGuid = false;
			database.instance.m_testTouch = true;
			m_pauseSprite.visible = true;
			ZoomSpriteOB(m_pauseSprite);
			if (m_pauseSprite.scaleX == 1)
			{
				m_rung7 = rung(m_pauseSprite, m_rung7, 2);
				ZoomSpriteOB(m_pauseButton);
				ZoomSpriteOB(m_pauseTextButton);
				ZoomSpriteOB(m_pauseButtonReTry);
				ZoomSpriteOB(m_pauseTextButtonReTry);
				if (m_pauseButton.scaleX == 1)
				{
					m_rung8 = rung(m_pauseButton, m_rung8, 1);
					m_rung9 = rung(m_pauseButtonReTry, m_rung9, 1);
				}
			}
		}
		
		public function release():void
		{
			m_list_path.length = 0;
			m_listBartrack.length = 0;
			m_listBaseBuild.length = 0;
			m_listCanon.length = 0;
			m_listEnemy.length = 0;
			m_listSolider.length = 0;
			m_listTower.length = 0;
			m_loadEnemy.release();
			
			removeChildren();
			this.m_testInit = false;
			this.m_testStart = false;
			this.visible = false;
			this.m_testplaybutton = false;
		}
		
		private function ZoomSprite():void
		{
			if (this.scaleX < 1)
			{
				this.scaleX += 0.05;
				this.scaleY = this.scaleX;
				for (var i:int = 0; i < m_listEnemy.length; i++)
				{
					m_listEnemy[i].visible = false;
				}
			}
			else
			{
				this.scaleX = 1;
				this.scaleY = 1;
				for (i = 0; i < m_listEnemy.length; i++)
				{
					m_listEnemy[i].visible = true;
				}
			}
		}
		
		private function ZoomSpriteObj(obj:Object):void
		{
			if (obj.scaleX < 1)
			{
				obj.scaleX += 0.05;
				obj.scaleY = obj.scaleX;
			}
			else
			{
				obj.scaleX = 1;
				obj.scaleY = 1;
				
			}
		}
		
		public function rung(obj:Object, _rung:int, _buzz:int):int
		{
			if (_rung != 0)
			{
				if (_rung > 0)
				{
					_rung -= 1;
				}
				obj.x += _rung;
				obj.y -= _rung;
				_rung *= -1;
				if (_rung == 4 && _buzz == 1)
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML()), Texture.fromBitmap(new AssetsParticles.ParticleTexture())), new Point(obj.x, obj.y), 50));
				if (_rung == 4 && _buzz == 2)
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML1()), Texture.fromBitmap(new AssetsParticles.ParticleTexture1())), new Point(obj.x, obj.y), 50));
				if (_rung == 4 && _buzz == 3)
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML2()), Texture.fromBitmap(new AssetsParticles.ParticleTexture4())), new Point(obj.x, obj.y), 50));
				return _rung;
			}
			return _rung;
		}
		
		private function setDefaultSprite():void
		{
			this.scaleX = 0.1;
			this.scaleY = 0.1;
			m_playButton.scaleX = 0;
			m_playButton.scaleY = 0;
			
			setScale(m_loseButton, 0, 0);
			setScale(m_loseTextButton, 0, 0);
			setScale(m_loseTextButtonReTry, 0, 0);
			setScale(m_loseButtonReTry, 0, 0);
			
			setScale(m_winButtonNext, 0, 0);
			setScale(m_winTextButtonNext, 0, 0);
		}
		
		public function disposeTemporarily():void
		{
			ManagerEffect.instance.ListEff.splice(0, ManagerEffect.instance.ListEff.length);
			this.removeChildren(0, this.numChildren, false);
			database.instance.m_Music.stopSound("GamePlay_Music");
			this.scaleX = 0.1;
			this.scaleY = 0.1;
			this.m_testInit = false;
			this.m_testStart = false;
			this.visible = false;
		
		}
		
		public function setCenterPivot(_object:Object):void
		{
			_object.pivotX = _object.width / 2;
			_object.pivotY = _object.height / 2;
		}
		
		public function setCenterPivotXY(_object:Object, _x:Number, _y:Number):void
		{
			_object.pivotX = _x;
			_object.pivotY = _y;
		}
		
		public function setPos(_object:Object, _x:int, _y:int):void
		{
			_object.x = _x;
			_object.y = _y;
		}
		
		public function setScale(_object:Object, _x:Number, _y:Number):void
		{
			_object.scaleX = _x;
			_object.scaleY = _y;
		}
		
		public function setSize(_object:Object, _x:int, _y:int):void
		{
			_object.width = _x;
			_object.height = _y;
		}
		
		private function ZoomSpriteOB(_object:Object):void
		{
			if (_object.scaleX < 1)
			{
				_object.scaleX += 0.05;
				_object.scaleY = this.scaleX;
			}
			else
			{
				_object.scaleX = 1;
				_object.scaleY = 1;
			}
		}
	}

}